package api

import (
	"fmt"
	"gitee.com/enzolwb/my-web-scaffold/models"
	"gitee.com/enzolwb/my-web-scaffold/pkg/app"
	"gitee.com/enzolwb/my-web-scaffold/pkg/app/validation"
	"gitee.com/enzolwb/my-web-scaffold/pkg/ecode"
	"github.com/gin-gonic/gin"
)

type RoleInput struct {
	RoleName string `form:"role_name" json:"role_name" binding:"required,max=20,min=2" comment:"角色名称"`
	Uri      string `form:"uri" json:"uri" binding:"required" comment:"URL"`
	Method   string `form:"method" json:"method" binding:"required" comment:"请求方式"`
}

func GetRole() {
	//获取该用户的role ID NAME URI
}

func AddPolicy(c *gin.Context) {
	fmt.Println("增加Policy")
	var (
		obj RoleInput
		err error
	)
	if err = validation.BindValidParams(c, &obj); err != nil {
		app.Response(c, ecode.ErrRequest)
		return
	}
	if ok := models.Enforcer.AddPolicy(obj.RoleName, obj.Uri, obj.Method); !ok {
		app.Response(c, ecode.ErrRequest.Reload("此策略已经添加过了"))
		return
	} else {
		app.Response(c, ecode.Ok)
		return
	}
}

func DelPolicy(c *gin.Context) {
	fmt.Println("删除Policy")

	if ok := models.Enforcer.RemovePolicy("admin", "/api/v1/hello", "GET"); !ok {
		app.Response(c, ecode.ErrRequest.Reload("Policy不存在"))
		return
	} else {
		app.Response(c, ecode.Ok)
		return
	}
}

func GetPolicy(c *gin.Context) {
	fmt.Println("查看policy")
	list := models.Enforcer.GetPolicy()
	for _, vlist := range list {
		for _, v := range vlist {
			fmt.Printf("value: %s, ", v)
		}
	}
}

func UpdatePolicy(c *gin.Context) {
	fmt.Println("更新policy")

}
